package function;

public class Power extends Function {
	private Function base;
	private Function exponent;

	public Power(Function b, Function e) {
		base = b;
		exponent = e;
	}

	public Function getBase() {
		return base;
	}

	public void setBase(Function base) {
		this.base = base;
	}

	public Function getExponent() {
		return exponent;
	}

	public void setExponent(Function exponent) {
		this.exponent = exponent;
	}

	public double evaluate(double x) {
		return Math.pow(base.evaluate(x), exponent.evaluate(x));
	}

	public Function differentiate() {
		return new Multiply(
				new Multiply(exponent, new Log(Math.E, base)).differentiate(),
				this);
	}

	public String toString() {
		if (base instanceof Constant
				&& ((Constant) base).getConstant() == Math.E)
			return "(e^" + exponent.toString() + ")";
		return "(" + base.toString() + "^" + exponent.toString() + ")";
	}

}
